# -*- coding: utf-8 -*-
"""
Created on Mon Apr 12 21:43:38 2021

@author: GPU_Tjdhf
"""
import os
from ftplib import FTP  # 引入ftp模块
from tjdutils.utils import get_file_list

class MyFtp():
    def __init__(self,host:str = '192.168.1.202',username:str=None,password:str=None,port=21,encoding:str = 'UTF-8',anonymous=0):
        '''
        :param anonymous: 默认需要用户名密码登陆
        '''
        self.ftp = FTP()
        self.ftp.connect(host,port)
        if anonymous==0:
            self.login(username,password)
        else:
            self.ftp.login()
            self.ftp.encoding = encoding

    def login(self,username,pwd):
        self.ftp.set_debuglevel(2)  # 打开调试级别2，显示详细信息
        self.ftp.login(username,pwd)
        self.ftp.encoding = encoding
        print(self.ftp.welcome)

    def downloadFile(self,remotepath,localpath):
        '''
        :param remotepath: ftp中的文件路径
        :param localpath: 本地保存路径
        :return: 
        '''
        os.chdir(localpath)   # 切换工作路径到下载目录

        filename=os.path.split(remotepath)[-1]#获取文件名称
        file_handle = open(filename,"wb").write   # 以写模式在本地打开文件
        self.ftp.retrbinary('RETR %s' % remotepath,file_handle,blocksize=1024)  # 下载ftp文件


    def uploadFile(self,localpath = None, remotepath = None):
        '''
        :param localpath:本地待上传文件路径 
        :param remotepath: ftp上要保存的文件夹路径
        :return: 
        '''
        self.ftp.cwd(remotepath)    
        # 以读模式在本地打开文件
        file_handle = open(localpath, 'rb')
        filename=os.path.split(localpath)[-1]   
        # 上传文件
        self.ftp.storbinary('STOR %s' %filename, file_handle, blocksize=1024)
        self.ftp.set_debuglevel(0)
        file_handle.close()
    

    def delete(self,remotepath,filename):
        '''
        删除ftp上指定的文件
        :param remotepath: ftp上待删除文件所在文件夹
        :param filename: ftp上待删除的文件名称
        :return: 
        '''
        self.ftp.cwd(remotepath)   # 要登录的ftp目录
        files = self.ftp.nlst()#获取当前目录下的所有文件
        #如果文件存在则删除
        if filename in files:
            self.ftp.delete(filename)  # 删除ftp服务器上的文件
            
    def get_file_list(self,remotepath):
        '''
        获取ftp上目录下所有文件名
        :remotepath: ftp上待查找文件名的目录
        '''
        self.ftp.cwd(remotepath)
        files = self.ftp.nlst()
        return files

    def close(self):
        self.ftp.set_debuglevel(0)  # 关闭调试
        self.ftp.quit()

def update_x_pkl(data_type:str="yoy", update_freq:str = 'low'):
    '''
    data_type:yoy or value
    update_freq:low（不定期更新，跑程序专用） or high（每日更新查询，最全数据集）
    '''
    ftp = MyFtp(host = '192.168.1.202',encoding = "GB2312",anonymous=1)
    if data_type == 'yoy':
        x_type = 'high_freq_all_data_2021'
    elif data_type == 'value':
        x_type = 'high_freq_all_data_value_2021'
    else:
        raise ValueError('data_type is error!')
    if update_freq == 'low':
        file_list = ftp.get_file_list("../基础数据/input/new")
        local_x_file = get_file_list('..\input',[])
        localpath = '../input'
        remotepath = '基础数据/input/new/'
        local_x_file = [item[9:] for item in local_x_file]
    elif update_freq == 'high':
        file_list = ftp.get_file_list("../基础数据/input/full")
        local_x_file = get_file_list('..\..\input',[])
        localpath = '../../input'
        remotepath = '基础数据/input/full/'
        x_type = x_type[:-4]+'full_'+x_type[-4:]
        local_x_file = [item[12:] for item in local_x_file]
    ftp.close()
    for item in file_list:
        if x_type in item:
            x_file = item
    if x_file not in local_x_file:
        ftp = MyFtp(host = '192.168.1.202',encoding = "GB2312",anonymous=1)
        ftp.downloadFile(remotepath + x_file, localpath)
        ftp.close()
    else:
        print('x 已经有最新的，不用更新！')           
    return x_file

def update_daily_pkl():
    ftp = MyFtp(host='192.168.1.202', encoding="GB2312", anonymous=1)
    files_list = ftp.get_file_list("../基础数据/每天更新特征")
    ftp.close()
    target_list = []
    for file in files_list:
        if len(file) == 14:
            target_list.append(file)
    target_list = sorted(target_list)
    target_file = target_list[-1]

    local_x_file = get_file_list('../input', [])
    local_x_file = [item[9:] for item in local_x_file]
    if target_file in local_x_file:
        print('日度更新文件已经有最新的，不用更新！')
    else:
        ftp = MyFtp(host='192.168.1.202', encoding="GB2312",anonymous=1)
        ftp.downloadFile('基础数据/每天更新特征/%s' %target_file, '../input')
        ftp.close()
        print(target_file + '更新完毕')
    return target_file

    
            
if __name__ == '__main__':
    x_name = update_x_pkl(data_type = 'yoy')
# =============================================================================
#     ftp = MyFtp(host = '192.168.1.202',encoding = "GB2312",anonymous=1)
#     #上传本地clean.txt文件到ftp_user文件夹
#     #ftp.uploadFile('../../input/x_in_set_selector.xlsx','../基础数据/input')
#     #将ftp上的clean.txt文件下载到本地
#     #ftp.downloadFile('基础数据/input/high_freq_all_data3_2021-03-10.pkl', './')
#     #删除ftp上/ftp_user/clean.txt文件
#     #ftp.delete('../基础数据/input', 'x_in_set_selector.xlsx')
#     #获取ftp上目录下所有文件名
#     file_list = ftp.get_file_list("../基础数据/input")
#     ftp.close()
# 
# =============================================================================
